package com.techstar.api.service.hinge_situation;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.techstar.api.pojo.hinge_situation.HingeSituationFreightRankVolume;
import com.techstar.api.pojo.hinge_situation.HingeSituationFreightVolume;
import com.techstar.api.repos.hinge_situation.HingeSituationFreightRankVolumeRepos;
import com.techstar.api.repos.hinge_situation.HingeSituationFreightVolumeRepos;
import com.techstar.api.service.ConfigProperties;
import com.techstar.api.util.GateWayApiProperties;
import com.techstar.api.util.HttpClientHeper;
import com.techstar.api.util.TrafficApiProperties;
import com.techstar.common.jackson.JacksonMapper;
import com.techstar.common.pojo.InterfaceType;
import com.techstar.common.utils.InterfaceUtils;
import com.techstar.roma.http.GatewayService;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.util.EntityUtils;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.net.URLEncoder;
import java.util.List;

/**
 * 
 * @author EinIce
 * @description 各枢纽货物吞吐量变化趋势
 * @date 2021年9月9日
 */
//@Service
//public class HingeSituationFreightVolumeService extends ParentOfListService<HingeSituationFreightVolume> {
//
//	@Autowired
//	private HingeSituationFreightVolumeRepos beanRepos;
//
//	@Autowired
//	private ConfigProperties configProperties; // 配置文件注入
//
//	public void query(String hub_type) {
//		try {
//		gateWayApiProperties = configProperties.romaHingeSituationApiProperties();
//		trafficApiProperties = configProperties.hsfvApiProperties();
//		String currentTime = URLEncoder.encode(DateTime.now().toString("yyyy-MM-dd HH:mm:ss"), "UTF-8").replaceAll("\\+", "%20");
//		trafficApiProperties.param = String.format("current_date=%s&end_date=%s&hub_type=%s", currentTime, currentTime, hub_type);
//		interfaceType = InterfaceType.HINGE_SITUATION_FREIGHT_VOLUME;
//		super.beanRepos = beanRepos;
//		sendHttp(new TypeReference<List<HingeSituationFreightVolume>>() {}, new String[] {"data"});
//		} catch (UnsupportedEncodingException e) {
//			e.printStackTrace();
//		}
//	}
//}
@Slf4j
@Service
public class HingeSituationFreightVolumeService {
    private static final ObjectMapper objectMapper = JacksonMapper.instance.objectMapper;
    @Autowired
    private HingeSituationFreightVolumeRepos beanRepos;

    @Autowired
    private ConfigProperties configProperties; // 配置文件注入

    public void query(String hub_type){
        try {
            GateWayApiProperties gateWayApiProperties = configProperties.romaHingeSituationApiProperties();
            TrafficApiProperties trafficApiProperties = configProperties.hsfvApiProperties();
            if(gateWayApiProperties.testMode==true&&!"test".equals(trafficApiProperties.able)
                    ||"disable".equals(trafficApiProperties.able)) {
                log.info("DistrictCongestTrendService out");
                return;
            }
            String currentTime = URLEncoder.encode(DateTime.now().toString("yyyy-MM-dd HH:mm:ss"), "UTF-8").replaceAll("\\+", "%20");
//            trafficApiProperties.param = String.format("end_date=%s&current_date=%s&hub_type=%s", currentTime, currentTime, hub_type);
            trafficApiProperties.param = String.format("current_date=%s&end_date=%s&hub_type=%s", currentTime, currentTime, hub_type);
            HttpGet httpGet = new HttpGet();
            httpGet = (HttpGet) GatewayService.GateWay(httpGet,gateWayApiProperties, trafficApiProperties);
            CloseableHttpResponse response = null;
            String result = null;
            response = HttpClientHeper.instance.httpclient.execute(httpGet);
            HttpEntity entity = response.getEntity();
            result = EntityUtils.toString(entity);
            log.info("各枢纽旅客运输量TOP排名" + result);
            JsonNode jsonNode = objectMapper.readTree(result.toString()).get("data");

            try {
                InterfaceType.HINGE_SITUATION_FREIGHT_VOLUME.setApiUrl(httpGet.toString());
                InterfaceType.HINGE_SITUATION_FREIGHT_VOLUME.setClassName(this.getClass().getSimpleName());
                InterfaceUtils.filtrateDate(InterfaceType.HINGE_SITUATION_FREIGHT_VOLUME, DateTime.now(), jsonNode);
            } catch (Exception e) {
                log.error("接口记录失败-"+this.getClass().getSimpleName()+":"+e.getMessage());
            }
            if(jsonNode==null)return;
            List<HingeSituationFreightVolume> list = objectMapper.readValue(jsonNode.get("freight_volume_info").toString(),new TypeReference<List<HingeSituationFreightVolume>>() {});
            beanRepos.saveAll(list);

            List<HingeSituationFreightVolume> list1 = objectMapper.readValue(jsonNode.get("freight_volume_info_year").toString(),new TypeReference<List<HingeSituationFreightVolume>>() {});
            beanRepos.saveAll(list1);

            List<HingeSituationFreightVolume> list2 = objectMapper.readValue(jsonNode.get("freight_volume_info_season").toString(),new TypeReference<List<HingeSituationFreightVolume>>() {});
            beanRepos.saveAll(list2);
            response.close();

        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());

        }
    }
}